requirejs.config({
    baseUrl:"src/js/",
    paths:{
        "jquery":["lib/jquery-3.3.1"],
        "storageTools":["lib/storageTools"],
        "jquery.fly.min":["lib/jquery.fly.min"],

        shim:{
            "jquery.fly.min":{
                deps: ["jquery"]
            }

        }
    }
});
require(["jquery","storageTools","jquery.fly.min"],function ($,tools) {
    $(function () {

        function getStyle(el, attr){
            if(el.currentStyle){
                return el.currentStyle[attr];
            }
            return window.getComputedStyle(el, null)[attr];
        }

//放大镜
$(function () {



            //核心公式    小区域/大区域=小图/大图


            //  小区域= (小图/大图) *大区域;
            //求出比例   小图/小区域=大图/大区域

            var oBox = document.querySelector('#box');

            //小区域
            var middleArea = document.getElementById('middleArea');
            //小图
            var middleImg = document.getElementById('middleImg');
            //大区域
            var bigArea = document.getElementById('bigArea');
            //大图
            var bigImg = document.getElementById('bigImg');//大图

            // 小区域 =(小图/大图)*大区域


            middleArea.style.width = (parseInt(getStyle(middleImg, "width")) / parseInt(getStyle(bigImg, "width"))) * parseInt(getStyle(bigArea, "width")) + "px";
            middleArea.style.height = (parseInt(getStyle(middleImg, "height")) / parseInt(getStyle(bigImg, "height"))) * parseInt(getStyle(bigArea, "height")) + "px";



            //如果当前 小区域是隐藏的  middleArea.offsetWidth 无法拿到宽度
            console.log(middleArea.offsetWidth);
            var oScale = middleImg.offsetWidth / parseInt(getStyle(middleArea, "width"))


            //鼠标进入小图,让小区域显示
            middleImg.onmouseenter = function(){
                $("#middleArea").show();
                $("#bigArea").show(800);
                //middleArea.style.display = "block";
                //bigArea.style.display = "block";
                document.onmousemove = function(evt){
                    var e = evt || window.event;

                    var mX = e.pageX - oBox.offsetLeft - middleImg.offsetLeft - middleArea.offsetWidth / 2;



                    if(mX <= 0){
                        mX = 0;
                    }
                    if(mX > middleImg.offsetWidth - middleArea.offsetWidth){
                        mX = middleImg.offsetWidth - middleArea.offsetWidth;
                    }
                    var mY =  e.pageY - oBox.offsetTop - middleImg.offsetTop - middleArea.offsetHeight / 2;


                    if(mY <= 0){
                        mY = 0;
                    }
                    if(mY > middleImg.offsetWidth - middleArea.offsetWidth){
                        mY = middleImg.offsetWidth - middleArea.offsetWidth;
                    }

                    middleArea.style.left = mX + "px";
                    middleArea.style.top = mY + "px";

                    //移动大图
                    bigImg.style.left = -oScale * mX + "px";
                    bigImg.style.top = -oScale * mY + "px";

                }
            }


            middleImg.onmouseleave = function(){
                document.onmousemove = null;
            }

            var oLis = document.querySelectorAll('#small li');
            for(var [k, o] of oLis.entries()){

                o.index = k;
                o.onclick = function(){
                    //alert(this.index);
//                 middleImg.children[0].src = `images/images2/${this.index + 1}_2.jpeg`;
//                 bigArea.children[0].src = `images/images2/${this.index + 1}_3.jpeg`;
                    middleImg.children[0].src = `src/images/ams${this.index + 1}.jpg`;
                    bigArea.children[0].src = `src/images/ams${this.index + 1}.jpg`;

                }

            }
            middleImg.onmouseleave = function(){
                $("#middleArea").hide();
                $("#bigArea").hide(400);
                document.onmousemove = null;
            }


        });
        $(function () {
            $(".footer").load('./footer.html',function () {
                //console.log("底部加载完成后执行");
            })
        })
        $(function () {

            var offset = $("#end").offset();  //结束的地方的元素
            //var num=document.getElementById("num");//数量++
            $("#xxx").on("click",function(event){   //是$(".addcar")这个元素点击促发的 开始动画的位置就是这个元素的位置为起点
                   num.value++;
                var btn2 = $(this);

                var img = btn2.parents(".boxta").find('img').attr('src');
                var flyer = $('<img class="u-flyer" src="'+img+'">');
                //飞
                flyer.fly({
                    //起点
                    start: {
                        left: event.pageX,
                        top: event.pageY
                    },
                    //终点
                    end: {
                        left: offset.left,
                        top: offset.top,
                        width: 0,
                        height: 0
                    },
                    //到达终点后调用的
                    onEnd: function(){
                        console.log("到达终点");
                        $("#msg").show().animate({width: '250px'}, 200).fadeOut(1000);
                    }
                });

            });
        })
        $(function () {
            $("#back-top").on("click", function () {
                $("html,body").animate({scrollTop: 0}, 200);
            })
        });
        $(function () {
            var jsonStr=tools.get("login1");
            //console.log(jsonStr);
            if(jsonStr!=null){
                $("#h4").show();
              $("#loginb").text('注销');
            }

            var userInfo=JSON.parse(jsonStr||"{}");

            $("#userinfor").text(userInfo.uname);
        })
        var rBtn=document.querySelector(".numjabtn");
        var lBtn=document.querySelector(".numjbtn");
        var num=document.getElementById("num");
        rBtn.onclick=function () {
            num.value++
        }
        lBtn.onclick=function () {
            num.value--;
            if(num.value<=1){
                num.value=1;
            }
        }
    })
    $(function () {
        $.ajax({
            url:"http://127.0.0.1:80/diwu/sever/goods.php",
            dataType:"json"


        }).done(function (res) {

        })
    })

})